package my2Cents.tools;

import java.awt.event.MouseEvent;
import java.util.Vector;

import net.phys2d.raw.Body;

import my2Cents.Util;
import my2Cents.properties.CheckBox;
import my2Cents.properties.Property;
import my2Cents.properties.Slider;

public class PhysProps extends AbstractPhysTool {
	private Vector<Integer> physProp_ints = new Vector<Integer>();
	private boolean gravity = true;
	
	public PhysProps() {
		setName("Physical Properties");
		setDescription("Adjusts physical properties for the\n" +
				"selected object.\n\n" +
				"Note: Physical properties for an object\n" +
				"are dependant on the object it collides with.");
		
		CheckBox check = new CheckBox("Gravity",true) {
			private static final long serialVersionUID = 1L;

			public void valueChanged(boolean b) {
				gravity = b;
			}
		};
		addProperty(check);
		addPhysProp("Restitution%", 4, 0, 100, 20);
		addPhysProp("Friction%", 4, 0, 100, 20);
		addPhysProp("Damping%", 0, 0, 100, 20);
		addPhysProp("Rotational Damping%", 3, 0, 100, 20);
		addPhysProp("Mass%", 100, 0, 2000, 200);
	}
	
	private void addPhysProp(String name, int def, int min, int max, int tickspace) {
		final int id = physProp_ints.size();
		physProp_ints.add(new Integer(def));
		Property p = new Slider(name,def,min,max,tickspace,true) {
			private static final long serialVersionUID = 1L;

			public void valueChanged(int value) {
				physProp_ints.set(id,new Integer(value));
			}
		};
		this.addProperty(p);		
	}
	
	private float getPhysProp(int index) {
		return physProp_ints.get(index).floatValue();
	}
	
	public void endTool(MouseEvent arg0) {
		Body b = Util.selectBody(arg0.getX(),arg0.getY(),getWorld());
		Util.clearHandle(getWorld());
/*		System.out.println(
		"Physical Properties:\n" +
		"Restitution: " + getPhysProp(0) + "\n" +
		"Friction: " + getPhysProp(1) + "\n" +
		"Damping: " + getPhysProp(2) + "\n" +
		"RotDamping: " + getPhysProp(3) + "\n");*/
		
		//+0.0001f, because 0.0 makes it break 0.o
		b.setRestitution((float) (getPhysProp(0)/100)+0.0001f);
		b.setFriction((float) (getPhysProp(1)/100)+0.0001f);
		b.setDamping((float) (getPhysProp(2)/100)+0.0001f);
		b.setRotDamping((float) (getPhysProp(3)/100)+0.0001f);
		b.setMassMultiplier((float) (getPhysProp(4)/100)+0.0001f);
		b.setGravityEffected(gravity);
		b.doOutlineFx();

		super.endTool(arg0);
	}
}
